package com.zq.it.dao.impl.admin;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.zq.it.dao.admin.ChapterDao;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.Chapter;

public class ChapterDaoImpl extends BaseDao<Chapter> implements ChapterDao {
  /*public static final Logger logger=t(ChapterDaoImpl.class);*/
	@Override
	public void saveChapterEty(Chapter academy){
    	getHibernateTemplate().save(academy);
	}
	@Override
	public void deleteChapterEtyByID(Integer id){	
		deleteEntity("delete Chapter as p where p.chapterId="+id);
	}

	@Override
	public List findChapterEtyList( final Integer id, final  String name){
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
					if(id!=null&&!name.equals(null))
					  return arg0.createCriteria(Chapter.class).add(org.hibernate.criterion.Restrictions.idEq(id)).setFetchMode("course",org.hibernate.FetchMode.JOIN).list();				    								  
			 return null;
			}						
		});
	}
	
	public Chapter findChapterFetchLs(Integer id){
		
		Chapter chapter=(Chapter) getSession().load(Chapter.class,id);		
		return chapter;
		
	}

	@Override
	public List findChapterEtyById(Integer id){
		return getHibernateTemplate().find("from Chapter where chapterId=?",id);
		// TODO Auto-generated method stub	
	}

	@Override
	public List findChapterEtyByName(String name) {
		return getHibernateTemplate().find("from Chapter as p where p.chapterName=?",name);
		// TODO Auto-generated method stub
	
	}

//	@Override
//	public void updateChapterByID(Integer id) {
//		getHibernateTemplate().update(entity);
//		// TODO Auto-generated method stub
//		
//	}

	@Override
	public void updateChapterEty(Chapter chapter) {
		getHibernateTemplate().update(chapter);
		// TODO Auto-generated method stub
		
	}
//	@Override
//	public List findChapterEtyByDate(Date date){
//		// TODO Auto-generated method stub
//		return getHibernateTemplate().find("from Chapter where createTime=?",date);
//	}
	@Override
	public List findChapterEtyByExmp(Chapter chapter){
		// TODO Auto-generated method stub
		return getHibernateTemplate().findByExample(chapter);
	}
	@Override
	public void deleteAllEty(int[] ids){
		org.springframework.orm.hibernate3.HibernateTemplate ht=getHibernateTemplate();
		for(int i :ids)		
		   ht.delete(ht.load(Chapter.class,i));         
	}
	@Override
	public void deleteChapterEtyByObj(Chapter chapter){
		getHibernateTemplate().delete(chapter);
	}
	@Override
	public void saveChapterEtyList(Collection<Chapter> collection){
		getHibernateTemplate().saveOrUpdateAll(collection);
	}
	@Override
	public List findChapterListByHQL(String hql){
		// TODO Auto-generated method stub
		return findEtyByHql(hql);
	}
	
	@Override
	public List findChapterAllEty(){
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session arg0)					 {
				return arg0.createCriteria(Chapter.class).setFetchMode("course",FetchMode.JOIN).list();
			}						
		});
	}
	@Override
	public List findChapterListBySQL(String sql){
		// TODO Auto-generated method stub
		return findEtyBySQL(sql);
	}
	@Override
	public List findChapterListEtyByFetchMode(String modeStr,String sql) {
		return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(Chapter.class).add(Restrictions.sqlRestriction(sql)).setFetchMode("course",FetchMode.JOIN));
		// TODO Auto-generated method stub
		//return findEtyByFetchMode(Chapter.class, modeStr,sql);
	}
	
}
